home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / others / ole_101.zip / PATRON.ZIP / FILEIO.C < prev    next >
C/C++ Source or Header  |  1992-04-13  |  7KB  |  257 lines

  1. /*
  2.  * FILEIO.C
  3.  *
  4.  * Functions for reading and writing .PTN files for this application.
  5.  *
  6.  * Copyright(c) Microsoft Corp. 1992 All Rights Reserved
  7.  *
  8.  */
  9.  
  10. #include <windows.h>
  11. #include <ole.h>
  12. #include "oclient.h"
  13. #include "blackbox.h"
  14. #include "patron.h"
  15.  
  16.  
  17.  
  18. /*
  19.  * FPtnFileRead
  20.  *
  21.  * Purpose:
  22.  *  Reads a .PTN file into a PATRON data structure.
  23.  *
  24.  * Parameters:
  25.  *  pszFile         LPSTR of the filename to read.
  26.  *  lpptn           LPPATRON to the structure to fill.
  27.  *  hWndParent      HWND of the parent window of all BlackBoxes
  28.  *  pDoc            LPDOCUMENT that owns the objects to create.
  29.  *
  30.  * Return Value:
  31.  *  BOOL            TRUE if the file was successfully read,
  32.  *                  FALSE otherwise.
  33.  */
  34.  
  35. BOOL FAR PASCAL FPtnFileRead(LPSTR pszFile, LPPATRON lpptn,
  36.                              HWND hWndParent, LPDOCUMENT pDoc)
  37.     {
  38.     HANDLE          hFile;
  39.     OFSTRUCT        of;
  40.     WORD            cbW;
  41.     DWORD           cObjects;
  42.     FILEOBJECT      fo;
  43.     LPOBJECT        pObj;
  44.     BOOL            fTemp;
  45.     HWND            hWnd;
  46.     OLESTATUS       os;
  47.  
  48.  
  49.     hFile=OpenFile(pszFile, &of, OF_READ);
  50.  
  51.     if (-1==hFile)
  52.         return FALSE;
  53.  
  54.     cbW=_lread(hFile, (LPVOID)lpptn, CBPATRON);
  55.  
  56.     if (CBPATRON!=cbW)
  57.         {
  58.         _lclose(hFile);
  59.         return FALSE;
  60.         }
  61.  
  62.     /*
  63.      * Put the file handle in the application's OLESTREAM structure
  64.      * so the Put method can see it.
  65.      */
  66.     pDoc->pStream->hFile=hFile;
  67.  
  68.     //Count the number of links that may need later updating.
  69.     pDoc->cLinks=0;
  70.  
  71.     //Read each object and create a window if necessary.
  72.     cObjects=lpptn->cObjects;
  73.  
  74.     while (cObjects)
  75.         {
  76.         cObjects--;
  77.  
  78.         //Read the FILEOBJECT structure for this object
  79.         cbW=_lread(hFile, (LPSTR)&fo, CBFILEOBJECT);
  80.  
  81.         if (CBFILEOBJECT!=cbW)
  82.             break;
  83.  
  84.         pObj=PObjectAllocate(&fTemp, pDoc);
  85.  
  86.         if (!fTemp)
  87.             {
  88.             PObjectFree(pDoc, pObj);
  89.             break;
  90.             }
  91.  
  92.         //Attempt to create an OLEOBJECT for this junk in the file.
  93.         os=OleLoadFromStream((LPOLESTREAM)pDoc->pStream, PSZOLE(IDS_STDFILEEDITING),
  94.                              (LPOLECLIENT)pObj, pDoc->lh, fo.szName, &pObj->pObj);
  95.  
  96.         /*
  97.          * Since we need to call other OLE functions for this object when
  98.          * we create a BlackBox window, we need to wait for each one
  99.          * individually.
  100.          */
  101.         if (OLE_OK!=OsError(os, pDoc, pObj, TRUE))
  102.             {
  103.             PObjectFree(pDoc, pObj);
  104.             break;
  105.             }
  106.  
  107.         //Create a window for this object with the stored information
  108.         hWnd=HBlackBoxCreate(hWndParent, &fo, TRUE, pObj);
  109.  
  110.         if (NULL==hWnd)
  111.             break;
  112.  
  113.         /*
  114.          * Try to update an automatic link.  If we can't then count another
  115.          * link to update later.
  116.          */
  117.         if (OT_LINK==pObj->dwType)
  118.             {
  119.             //Count a manual or non-updated auto link
  120.             if (!FObjectAutoLinkUpdate(pDoc, pObj))
  121.                 pDoc->cLinks++;
  122.             }
  123.         }
  124.  
  125.     _lclose(hFile);
  126.  
  127.     //Activate the first window we created.
  128.     hWnd=GetWindow(hWndParent, GW_CHILD);
  129.  
  130.     if (NULL!=hWnd)
  131.         {
  132.         BringWindowToTop(hWnd);
  133.         SendMessage(hWnd, WM_NCACTIVATE, TRUE, 0L);
  134.         }
  135.  
  136.     return TRUE;
  137.     }
  138.  
  139.  
  140.  
  141.  
  142.  
  143. /*
  144.  * FPtnFileWrite
  145.  *
  146.  * Purpose:
  147.  *  Writes a .PTN file in a PATRON structure pointed to by lpptn.
  148.  *
  149.  * Parameters:
  150.  *  pszFile         LPSTR of the filename to read.
  151.  *  lpptn           LPPATRON pointing to the structure to write,
  152.  *  pDoc            LPDOCUMENT that owns the objects.
  153.  *
  154.  * Return Value:
  155.  *  BOOL            TRUE if the file was successfully written,
  156.  *                  FALSE otherwise.
  157.  */
  158.  
  159. BOOL FAR PASCAL FPtnFileWrite(LPSTR pszFile, LPPATRON lpptn, LPDOCUMENT pDoc)
  160.     {
  161.     HANDLE          hFile;
  162.     OFSTRUCT        of;
  163.     DWORD           cb;
  164.     BOOL            fOK;
  165.  
  166.     hFile=OpenFile(pszFile, &of, OF_CREATE | OF_WRITE);
  167.  
  168.     if (-1==hFile)
  169.         return FALSE;
  170.  
  171.     cb=_lwrite(hFile, (LPSTR)lpptn, CBPATRON);
  172.  
  173.     if ((DWORD)CBPATRON!=cb)
  174.         {
  175.         _lclose(hFile);
  176.         return FALSE;
  177.         }
  178.  
  179.     /*
  180.      * Put the file handle in the application's OLESTREAM structure
  181.      * so the Put method can see it.
  182.      */
  183.     pDoc->pStream->hFile=hFile;
  184.  
  185.     //Go write all the objects.
  186.     fOK=FObjectsEnumerate(pDoc, FEnumFileWrite, 0L);
  187.     _lclose(hFile);
  188.  
  189.  
  190.     /*
  191.      * Leave the call to OleSavedClientDoc to the caller of this function.
  192.      * We don't know anything about documents here, just objects.
  193.      */
  194.     return fOK;
  195.     }
  196.  
  197.  
  198.  
  199.  
  200. /*
  201.  * FEnumFileWrite
  202.  *
  203.  * Purpose:
  204.  *  Enumeration callback function for use from FPtnFileWrite when it
  205.  *  calls FObjectsEnumerate.  For each object we save the FILEOBJECT
  206.  *  header for the window and call OleSaveToStream for it's object.
  207.  *
  208.  * Parameters:
  209.  *  pDoc            LPDOCUMENT identifying the owner of all objects.
  210.  *  pObj            LPOBJECT identifying the current object.
  211.  *  dw              DWORD for extra data, unused.
  212.  *
  213.  * Return Value:
  214.  *  BOOL            TRUE to continue the enumeration, FALSE otherwise.
  215.  */
  216.  
  217. BOOL FAR PASCAL FEnumFileWrite(LPDOCUMENT pDoc, LPOBJECT pObj, DWORD dw)
  218.     {
  219.     FILEOBJECT      fo;
  220.     OLESTATUS       os;
  221.     HWND            hWnd;
  222.     WORD            cb;
  223.  
  224.     hWnd=(HWND)pObj->hData;
  225.  
  226.     //Retrieve the window rectangle for this blackbox and write it.
  227.     SendMessage(hWnd, BBM_RECTGET, 0, (LONG)(LPSTR)&fo.rc);
  228.     GetWindowText(hWnd, fo.szName, 40);
  229.     fo.wID=GetWindowWord(hWnd, GWW_ID);
  230.  
  231.     /*
  232.      * Saving the size of an object is useful to skip over objects
  233.      * on a quick file scan.  For example, if you want to quickly open
  234.      * a file without showing pictures, you could read the FILEOBJECT
  235.      * header and seek past the OLE object.  You could then go back
  236.      * later and call OleLoadFromStream.
  237.      *
  238.      * Here, if we cannot get the size, we fail the file write.
  239.      */
  240.     os=OleQuerySize(pObj->pObj, &fo.cbObject);
  241.  
  242.     if (OLE_OK!=os)
  243.         return FALSE;
  244.  
  245.     //Write the object header.
  246.     cb=_lwrite(pDoc->pStream->hFile, (LPSTR)&fo, CBFILEOBJECT);
  247.  
  248.     if (CBFILEOBJECT!=cb)
  249.         return FALSE;
  250.  
  251.     //Write the actual OLE object.
  252.     os=OleSaveToStream(pObj->pObj, (LPOLESTREAM)pDoc->pStream);
  253.  
  254.     //Stop enumeration if there's an error.
  255.     return (OLE_OK==os);
  256.     }
  257.